package com.iweb.controller;

import com.iweb.pojo.User;
import com.iweb.service.PlaceService;
import com.iweb.service.UserService;
import com.iweb.utils.IsNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    PlaceService placeService;

    /*@RequestMapping(value = "deleteUser")
    public String deleteUser(HttpServletRequest req,int id ) {
        List userLists = userService.selectList();
        System.out.println(userLists);
        req.setAttribute("userLists",userLists);
        return "/admin/personnel/user";
    }*/
    @RequestMapping(value = "queryUser")
    public String queryUser(HttpServletRequest req) {

        List userLists = userService.selectList();
        System.out.println(userLists);
        req.setAttribute("userLists",userLists);
        return "/admin/personnel/user";
    }

    @RequestMapping(value = "updatePassword", method = RequestMethod.GET)
    public String updatePassword() {
        return "user/usercenter/resetpwd";
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public String updatePassword(HttpServletRequest req, User user) {
        user.setUserPhone(req.getSession().getAttribute("userPhone").toString());
        System.out.println(user);
        if (userService.updatePassword(user)) {
            String newPassword = userService.queryPassword(user);
            req.setAttribute("message", "密码修改成功,新密码为: " + newPassword);
            return "user/login";
        }
        req.setAttribute("message", "密码修改失败");
        return "user/usercenter/resetpwd";
    }

    @RequestMapping(value = "/logOut")
    public String logOut(HttpServletRequest req, User user) {
        req.getSession().removeAttribute("userID");
        req.getSession().removeAttribute("userName");
        req.getSession().removeAttribute("userPhone");
        return "user/login";
    }

    @RequestMapping(value = "/adminLogOut")
    public String adminLogOut(HttpServletRequest req, User user) {
        req.getSession().removeAttribute("userID");
        req.getSession().removeAttribute("userName");
        req.getSession().removeAttribute("userPhone");
        return "/admin/index";
    }

    //修改用户信息，跳回jsp，并显示该用户相应信息
    @RequestMapping(value = "alterInfo")
    public String updatePersonalInfo(HttpServletRequest req) {
        String userPhone = req.getSession().getAttribute("userPhone").toString();
        System.out.println(userPhone);
        User user = userService.selectByPhone(userPhone);
        System.out.println(user);
        req.setAttribute("user", user);
        return "user/usercenter/alterinfo";
    }

    //确认修改用户信息
    @RequestMapping(value = "updatePersonalInfo", method = RequestMethod.POST)
    public String updatePersonalInfo(HttpServletRequest req, User user) {
        System.out.println(user);
        if (userService.updatePersonalInfo(user)) {
            List list = userService.selectUserInfo(user.getUserID());
            req.setAttribute("users", list);
            //用户个人中心
            return "user/usercenter/index";
        }
        req.setAttribute("message", "出了点小问题,请重新操作");
        return "user/usercenter/alterinfo";
    }

    @RequestMapping(value = "queryUserInfo" )
    public String queryUserInfo(HttpServletRequest req){
        int userID = Integer.parseInt(req.getSession().getAttribute("userID").toString());
        List list = userService.queryUserInfo(userID);
        req.setAttribute("users", list);
        //用户个人中心
        return "user/usercenter/index";

    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "user/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest req, User user) {
        //查询，连同category表一起
        List list = userService.login(user);
        if (list != null && list.size() != 0) {
            if (list.get(0).toString().contains("POWERID=1")) {
                //session设置
                User user1 = userService.selectByPhone(user.getUserPhone());
                req.getSession().setAttribute("userID", user1.getUserID());
                req.getSession().setAttribute("userName", user1.getUserName());
                req.getSession().setAttribute("userPhone", user1.getUserPhone());
                req.setAttribute("users", list);
                //用户个人中心
                return "user/usercenter/index";
            }
            if (list.get(0).toString().contains("POWERID=2")) {
                User user1 = userService.selectByPhone(user.getUserPhone());
                req.getSession().setAttribute("userID", user1.getUserID());
                req.getSession().setAttribute("userName", user1.getUserName());
                req.getSession().setAttribute("userPhone", user1.getUserPhone());
                req.setAttribute("admins", list);
                return "/admin/index";
            }
        }
        req.setAttribute("message", "用户名或密码错误");
        return "user/login";

    }

    @RequestMapping(value = "delete")
    public void deleteAdmin(HttpServletRequest req,HttpServletResponse resp ,int userID) throws IOException {
        PrintWriter out = resp.getWriter();
        if ( userService.delete(userID)){
            out.print("success");
        }else {
            out.print("error");
        }
    }

    @RequestMapping(value = "queryAdmin")
    public String queryAdmin(HttpServletRequest req){
        List list = userService.queryAdmin(2);
        System.out.println(list);
        req.setAttribute("adminLists",list);
        return "admin/personnel/admin";
    }

    @RequestMapping(value = "addAdmin", method = RequestMethod.GET)
    public String addAdmin() {
        return "admin/personnel/addAdmin";
    }

    @RequestMapping(value = "addAdmin", method = RequestMethod.POST)
    public String addAdmin(HttpServletResponse resp, HttpServletRequest req, User user) {
        user.setPowerId(2);
        System.out.println(user);
        if (userService.addAdmin(user)){return "admin/index"; }
        req.setAttribute("message","注册失败");
        return "admin/personnel/addAdmin";

    }



    @RequestMapping(value = "addUser", method = RequestMethod.GET)
    public String addUser() {
        return "user/usercenter/register";
    }

    @RequestMapping(value = "addUser", method = RequestMethod.POST)
    public String addUser(HttpServletResponse resp, HttpServletRequest req, User user) {
        System.out.println(user);
        if (userService.addUser(user)) {
            req.setAttribute("message", "注册成功，您的账号:" + user.getUserPhone() + "密码:" + user.getPassword());
            return "user/login";
        }
        req.setAttribute("message", "注册失败");
        return "user/usercenter/register";
    }

    @RequestMapping(value = "selectPhone")
    public void selectPhone(HttpServletResponse resp, String userPhone) throws IOException {
        User newUser = userService.selectPhone(userPhone);
        System.out.println(userPhone);
        System.out.println(newUser);
        PrintWriter out = resp.getWriter();
        if (IsNumber.isNumber(userPhone)){
            if (newUser == null) {
                out.println("success");
            } else {
                out.println("error02");
            }
        } else {
            out.print("error01");
        }
    }
    @RequestMapping(value = "/recharge",method = RequestMethod.GET)
    public String recharge(HttpServletRequest req) {
        int userID = Integer.parseInt(req.getSession().getAttribute("userID").toString());
        List list = userService.selectListByID(userID);
        req.setAttribute("list",list);
        return "user/usercenter/recharge";
    }
    @RequestMapping(value = "/recharge",method = RequestMethod.POST)
    public String recharge(HttpServletRequest req,User user) {
        int userID = Integer.parseInt(req.getSession().getAttribute("userID").toString());
        user.setUserID(userID);
        userService.recharge(user);
        return "success";
    }


    @RequestMapping(value = "/addCategoryLevel")
    public String addCategoryLevel() {
        return "addLevel";
    }

    @RequestMapping(value = "/updateCategoryLevel")
    public String updateCategoryLevel(User user ,HttpServletRequest req) {
        user.setUserID(Integer.parseInt(req.getSession().getAttribute("userID").toString()));
        System.out.println(user);
        userService.updateCategoryLevel(user);
        return "success";
    }


}
